package com.oamic.xiaomi.serverproject.controller;

import com.oamic.xiaomi.serverproject.entity.FaultReport;
import com.oamic.xiaomi.serverproject.service.FaultReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/faults")
@CrossOrigin(origins = "http://localhost:8082")
public class FaultReportController {
    
    @Autowired
    private FaultReportService faultReportService;
    
    /**
     * 获取所有故障报告
     * @return 故障报告列表
     */
    @GetMapping
    public List<FaultReport> getAllFaultReports() {
        return faultReportService.getAllFaultReports();
    }
    
    /**
     * 根据ID获取故障报告
     * @param id 故障报告ID
     * @return 故障报告
     */
    @GetMapping("/{id}")
    public FaultReport getFaultReportById(@PathVariable Long id) {
        return faultReportService.getFaultReportById(id);
    }
    
    /**
     * 创建故障报告
     * @param faultReport 故障报告信息
     * @return 是否创建成功
     */
    @PostMapping
    public boolean createFaultReport(@RequestBody FaultReport faultReport) {
        return faultReportService.createFaultReport(faultReport);
    }
    
    /**
     * 更新故障报告
     * @param id 故障报告ID
     * @param faultReport 故障报告信息
     * @return 是否更新成功
     */
    @PutMapping("/{id}")
    public boolean updateFaultReport(@PathVariable Long id, @RequestBody FaultReport faultReport) {
        faultReport.setId(id);
        return faultReportService.updateFaultReport(faultReport);
    }
    
    /**
     * 删除故障报告
     * @param id 故障报告ID
     * @return 是否删除成功
     */
    @DeleteMapping("/{id}")
    public boolean deleteFaultReport(@PathVariable Long id) {
        return faultReportService.deleteFaultReport(id);
    }
    
    /**
     * 更新故障处理状态
     * @param id 故障报告ID
     * @param status 处理状态
     * @param resolution 处理结果
     * @return 是否更新成功
     */
    @PatchMapping("/{id}/status")
    public boolean updateFaultStatus(@PathVariable Long id, 
                                     @RequestParam FaultReport.FaultStatus status,
                                     @RequestParam(required = false) String resolution) {
        return faultReportService.updateFaultStatus(id, status, resolution);
    }
}